//
//  ViewController.m
//  第二题
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "CreateImageView.h"

/*
 使 UIImageView实现 <植物大战僵尸>:
 需求:
 1. 三种植物实现动态图
 2. 三个僵尸实现走动的动态图 
 3. (不要求有移动的效果)
 */

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self creatImage];
    [self animationOfPlants];
    [self animationOfGua];
    [self animationOfFlower];
    [self animationOfZombie];
}

#pragma mark - 背景
-(void)creatImage
{
    UIImage *image = [UIImage imageNamed:@"BackGround.png"];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    imageView.frame = [UIScreen mainScreen].bounds;
    
    [self.view addSubview:imageView];
    [imageView release];
}

#pragma mark - 第一个植物
-(void)animationOfPlants
{
    CreateImageView *animationImageView = [CreateImageView initWithFrame:CGRectMake(50, 250, 100, 100) withName:@"plants1.tiff"];
    [self.view addSubview:animationImageView];
    [animationImageView release];
    
    //调用
    [self createAnimationWithName:@"plants" withNum:10 withView:animationImageView];
    
}

#pragma mark - 第二个植物
-(void)animationOfGua
{
    CreateImageView *animationImageView = [CreateImageView initWithFrame:CGRectMake(50, 100, 100, 100) withName:@"gua1.tiff"];
    [self.view addSubview:animationImageView];
    [animationImageView release];
    
    //调用
    [self createAnimationWithName:@"gua" withNum:17 withView:animationImageView];
}

#pragma mark - 第三个植物
-(void)animationOfFlower
{
    CreateImageView *animationImageView = [CreateImageView initWithFrame:CGRectMake(50, 450, 100, 100) withName:@"flower1.tiff"];
    [self.view addSubview:animationImageView];
    [animationImageView release];
    
    //调用
    [self createAnimationWithName:@"flower" withNum:19 withView:animationImageView];
}

#pragma mark - 僵尸
-(void)animationOfZombie
{
#pragma mark - 第一个
    CreateImageView *animationImageView = [CreateImageView initWithFrame:CGRectMake(230, 30, 200, 200) withName:@"BZombie1.tiff"];
    [self.view addSubview:animationImageView];
    [animationImageView release];
    
    //调用
    [self createAnimationWithName:@"BZombie" withNum:26 withView:animationImageView];
    
#pragma mark - 第二个
    CreateImageView *animationImageView1 = [CreateImageView initWithFrame:CGRectMake(230, 150, 200, 200) withName:@"BZombie1.tiff"];
    [self.view addSubview:animationImageView1];
    [animationImageView1 release];
    
    //调用
    [self createAnimationWithName:@"BZombie" withNum:26 withView:animationImageView1];
    
#pragma mark - 第三个
    CreateImageView *animationImageView2 = [CreateImageView initWithFrame:CGRectMake(230, 350, 200, 200) withName:@"BZombie1.tiff"];
    [self.view addSubview:animationImageView2];
    [animationImageView2 release];
    
    //调用
    [self createAnimationWithName:@"BZombie" withNum:26 withView:animationImageView2];
}

#pragma mark - 动画
-(void)createAnimationWithName:(NSString *)name withNum:(NSInteger)num withView:(UIImageView *)imageView
{
    NSMutableArray *array = [NSMutableArray array];
    
    for (int i = 1; i < num; i++) {
        NSString *newName = [NSString stringWithFormat:@"%@%d.tiff", name, i];
        UIImage *image = [UIImage imageNamed:newName];
        [array addObject:image];
    }
    imageView.animationImages = array;
    imageView.animationDuration = 0;
    [imageView startAnimating];
}

@end
















